<?php
header("Content-type: text/html; charset=utf-8");

include "TopSdk.php";

function litimgurls($imgid=0)
{
    global $lit_imglist,$dsql;
    //获取附加表
    $row = $dsql->GetOne("SELECT c.addtable FROM #@__archives AS a LEFT JOIN #@__channeltype AS c 
                                                            ON a.channel=c.id where a.id='$imgid'");
    $addtable = trim($row['addtable']);
    
    //获取图片附加表imgurls字段内容进行处理
    $row = $dsql->GetOne("Select imgurls From `$addtable` where aid='$imgid'");
    
    //调用inc_channel_unit.php中ChannelUnit类
    $ChannelUnit = new ChannelUnit(2,$imgid);
    
    //调用ChannelUnit类中GetlitImgLinks方法处理缩略图
    $lit_imglist = $ChannelUnit->GetlitImgLinks($row['imgurls']);
    
    //返回结果
    return $lit_imglist;
}
/*
 传入文章输入的关键词字符串根据传入下标提取一个关键词并返回
 $keywords 传入的关键词字符串
 $index 传入的想要通过下标获取的某个关键词
*/
function getKeywordByIndex($keywords,$index){
	$keywords = $keywords;
	$keywordsArr = explode(",", $keywords);
	$count  =  count($keywordsArr); 
	if($count<1){
		return "减肥茶";//没有关键词则默认一个关键词
	}else{
		return $keywordsArr[$index];
	}
	
}
/***************** 以下信息用于淘客调用**********************/
/**
以数组的形式返回淘客需要的图片链接，点击链接等信息
*/
function getTaoBaokeInfo($keyword){
	//从全局配置变量里面获取所需数据
	global $dsql;
	$tablename=hnx_sysconfig;
	$taoke_pid = $dsql->GetOne("SELECT value FROM $tablename  WHERE varname ='taoke_pid'");
	$taoke_app_key = $dsql->GetOne("select value from $tablename where varname ='taoke_app_key'");
	$taoke_app_secret = $dsql->GetOne("select value from $tablename where varname ='taoke_app_secret'");
	$taoke_start_credit = $dsql->GetOne("select value from $tablename where varname ='taoke_start_credit'");
	$taoke_end_credit = $dsql->GetOne("select value from $tablename where varname ='taoke_end_credit'");
	$taoke_sort = $dsql->GetOne("select value from $tablename where varname ='taoke_sort'");
	$taoke_page_size = $dsql->GetOne("select value from $tablename where varname ='taoke_page_size'");
	$taoke_start_commissi = $dsql->GetOne("select value from $tablename where varname ='taoke_start_commissi'");
	$take_end_commissionR = $dsql->GetOne("select value from $tablename where varname ='take_end_commissionR'");
	$taoke_start_totalnum = $dsql->GetOne("select value from $tablename where varname ='taoke_start_totalnum'");
	$taoke_end_totalnum = $dsql->GetOne("select value from $tablename where varname ='taoke_end_totalnum'");
	$taoke_mall_item = $dsql->GetOne("select value from $tablename where varname ='taoke_mall_item'");
	
	//签名用的
	$app_key = $taoke_app_key['value'];
	$secret=$taoke_app_secret['value'];
	$timestamp=time()."000"; 
	$message = $secret.'app_key'.$app_key.'timestamp'.$timestamp.$secret; 
	$mysign=strtoupper(hash_hmac("md5",$message,$secret)); 
	setcookie("timestamp",$timestamp); 
	setcookie("sign",$mysign); 
	
	$c = new TopClient;
	$c->appkey = $app_key;
	$c->secretKey = $secret;
	$req = new TaobaokeItemsGetRequest;
	$req->setFields("title,pic_url,price,click_url");
	$req->setPid($taoke_pid['value']);
	$req->setKeyword($keyword);
	$req->setStartCredit($taoke_start_credit['value']);
	$req->setEndCredit($taoke_end_credit['value']);
	$req->setSort($taoke_sort['value']);
	$req->setStartCommissionRate($taoke_start_commissi['value']);
	$req->setEndCommissionRate($take_end_commissionR['value']);
	$req->setStartTotalnum($taoke_start_totalnum['value']);
	$req->setEndTotalnum($taoke_end_totalnum['value']);
	$req->setMallItem($taoke_mall_item['value']);
	$req->setPageNo(1);
	$req->setPageSize($taoke_page_size['value']);
	$resp = $c->execute($req);
	
	$randNum=rand(0,$taoke_page_size['value']-1);
	
	$picurl=($resp->taobaoke_items->taobaoke_item[$randNum]->pic_url);
	$clickurl=($resp->taobaoke_items->taobaoke_item[$randNum]->click_url);
	$results=array("picurl"=>$picurl,"clickurl"=>$clickurl);
	return $results;
	
}